import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@WebServlet("/stringGuess")
public class GuessNumServlet extends HttpServlet {
    private int toGuess = 0;
    private Random random = new Random();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 这个方法用来从服务器获取猜数字的初始页面
        resp.setContentType("text/html;charset=utf8");
        // [0,100) --> +1 -->[0,100]
        toGuess = random.nextInt(100) + 1;
        System.out.println(toGuess);
        resp.getWriter().write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>猜数字</title>\n" +
                "</head>\n" +
                "\n" +
                "<body>\n" +
                "    <h3>猜数字</h3>\n" +
                "    <div>\n" +
                "        <form action=\"guessNum\" method=\"Post\">\n" +
                "            <input type=\"text\" name=\"toGuess\">\n" +
                "            <input type=\"submit\" value=\"猜\">\n" +
                "\n" +
                "        </form>\n" +
                "    </div>\n" +
                "    <div>\n" +
                "        结果：\n" +
                "    </div>\n" +
                "</body>\n" +
                "\n" +
                "</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 这个方法用来从服务器获取到“带有结果" 的页面
        resp.setContentType("text/html;charset=utf8");
        // 这里就要根据用户提交的数据来比较一下结果是啥了,讲字符串装换成整数
        int num = Integer.parseInt(req.getParameter("toGuess"));
        String result = "";
        if (num > toGuess) {
            result = "猜大了";
        } else if (num < toGuess) {
            result = "猜小了";
        } else {
            result = "猜对了";
        }
        resp.getWriter().write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>猜数字</title>\n" +
                "</head>\n" +
                "\n" +
                "<body>\n" +
                "    <h3>猜数字</h3>\n" +
                "    <div>\n" +
                "        <form action=\"guessNum\" method=\"Post\">\n" +
                "            <input type=\"text\" name=\"toGuess\">\n" +
                "            <input type=\"submit\" value=\"猜\">\n" +
                "\n" +
                "        </form>\n" +
                "    </div>\n" +
                "    <div>\n" +
                "        结果：\n" + result +
                "    </div>\n" +
                "</body>\n" +
                "\n" +
                "</html>");
    }
}
